package com.yaozheng.vocationaltraining.service.impl;

import android.text.TextUtils;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IAddQuestionsView;
import com.yaozheng.vocationaltraining.iview.IReplyView;
import com.yaozheng.vocationaltraining.service.ReplyService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.ImageUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.EBean;

import java.io.IOException;
import java.util.HashMap;

/**
 * Created by moon on 2017/11/12.
 */

@EBean
public class ReplyServiceImpl implements ReplyService {
    IReplyView iTokenBaseView;

    @Override
    public void init(IReplyView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;
    }

    @Override
    public void reply(final String body, String image,final int questionId) {
        try {
            if (!TextUtils.isEmpty(image)) {
                byte[] uploadContent = ImageUtils.bitmap2Bytes(ImageUtils
                        .revitionImageSize(image));
                if (uploadContent == null) {
                    iTokenBaseView.replyError("上传图片失败！！！");
                }
                OkHttpClientManager.getInstance().upload(
                        Constants.ROOTURL + "/knowledge/image/add",
                        iTokenBaseView.getToken(),
                        new OkHttpClientManager.IBaseViewCallback(new BaseView(iTokenBaseView) {
                            @Override
                            public void responseSuccess(JSONObject jsonObject) {
                                JSONObject imageObject = TypeUtils.getJsonObject(jsonObject, "data");

                                replyIng(body,
                                        imageObject.getString("image"),questionId);
                            }

                            @Override
                            public void responseError(String error) {
                                iTokenBaseView.replyError(error);
                            }

                            @Override
                            public boolean isResponseResult() {
                                return iTokenBaseView.isResponseResult();
                            }

                        }), uploadContent);
            }else{
                replyIng(body,"",questionId);

            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView.replyError("回复失败");
            }

        }

    }

    public void replyIng(String body, String image,int questionId){
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("body", body);
            if (!TextUtils.isEmpty(image)){
                para.put("images",image);
            }

            JSONObject json = new JSONObject();
            json.put("body", body);

            if (!TextUtils.isEmpty(image)){
//                ["http://api.borgwardapp.com/app/question/2017/10/fb0184a7c8cc2a2d2f76e7000b8b0f9a.jpg"]
                para.put("images",image);
                JSONArray array = new JSONArray();
                array.add(image);
                json.put("images",array);
            }

            OkHttpClientManager.getInstance().postJson(
                    Constants.ROOTURL + "/knowledge/question/"+questionId+"/answer/add",
                    iTokenBaseView.getToken(), json.toString(), new Callback() {
                        @Override
                        public void onFailure(Request request, IOException e) {
                            iTokenBaseView.replyError("回答问题失败");
                        }

                        @Override
                        public void onResponse(Response response) throws IOException {
                            iTokenBaseView.replySuccess(null);
                        }
                    });

//            OkHttpClientManager.getInstance().post(
//                    Constants.ROOTURL + "/knowledge/question/"+questionId+"/answer/add", iTokenBaseView.getToken(), para, new BaseView(iTokenBaseView) {
//                        @Override
//                        public void responseSuccess(JSONObject jsonObject) {
//                            iTokenBaseView.replySuccess(jsonObject);
//                        }
//
//                        @Override
//                        public void responseError(String error) {
//                            iTokenBaseView.replyError(error);
//                        }
//                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .replyError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }


}
